#!/bin/sh
port=8256

if [ x$1 = "x" -o y$2 = "y" ];
then
    echo "********************************************************************************************"
    echo "Error: Please enter the environment name and session serial"
    echo "Supported environments: dev, st, uat, sim, prod"
    echo "Example:"
    echo "./start.sh prod 1"
    echo "********************************************************************************************"
	exit 1
fi

echo "********************************************************************************************"
mkdir -p ../logs

outfile="../logs/process.log"
if [ -f $outfile ];
then
    rm $outfile
fi

for((i=0;i<$2;i++))
{
    echo "Starting lzk-demo instance [$(( i + 1))] for [$1] environment at port [$port]..."
    java -Xms6g -Xmx8g -XX:MetaspaceSize=1024m -XX:MaxMetaspaceSize=2048m -XX:MaxNewSize=2048m -jar ../lib/lzk-demo-*.jar --server.port=$port --logging.path=../logs/process_$((i + 1)) --spring.config.location=file:../cfg/im_config_$1.yml >> $outfile 2>&1 &
    ((port = port + 1))
}

for((i=0;i<30;i++))
{
    echo -ne "."
    sleep 1
}

cat $outfile

ps -fu $USER | grep -w "lzk-demo" | grep -v grep |awk '{print "Running Service - PID ["$2"] - Start Time ["$5"] - ["$11"] - ["$12"]"}'

serviceCount=($(ps -fu $USER | grep -w "lzk-demo" | grep -v grep | wc -l))
echo "Total service count [$serviceCount]"

#if check_if_process_is_running
#then
#    echo  -e  "\nProcess started successfully"
#    ps -fu $USER | grep -w "lzk-demo" | grep -v grep |awk '{print "Process Info - PID ["$2"] - Start Time ["$5"] - ["$11"]"}'
#    echo "********************************************************************************************"
#    exit 0
#else
#    echo  -e "\nERROR: Process could not be started, please see the logs for details"
#    echo "********************************************************************************************"
#    exit 1
#fi

echo "********************************************************************************************"


